package com.tanhua.server.service;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import com.tanhua.dubbo.server.api.RecommendUserApi;
import com.tanhua.dubbo.server.pojo.RecommendUser;
import com.tanhua.dubbo.server.vo.PageInfo;
import com.tanhua.server.vo.TodayBest;
import org.springframework.stereotype.Service;

/**
 * 负责与dubbo服务进行交互,进行远程调用
 */
@Service
public class RecommendUserService {

    @Reference(version = "1.0.0")
    private RecommendUserApi recommendUserApi;

    /**
     * 查看今日佳人
     *
     * @param id
     * @return
     */
    public TodayBest findTodayBest(Long id) {
        //通过远程调用的方式获取今日佳人的数据
        RecommendUser recommendUser = recommendUserApi.findWithMaxScore(id);
        if (null == recommendUser) {
            //如果查询结果为空返回null
            return null;
        }
        //封装一个todaybest对象,属性值使用通过远程调用获取的今日佳人数据的属性值
        TodayBest todayBest = new TodayBest();
        todayBest.setId(recommendUser.getUserId());
        //缘分值
        todayBest.setFateValue(Double.valueOf(Math.floor(recommendUser.getScore())).longValue());
        return todayBest;
    }

    /**
     * 查看推荐按列表
     *
     * @param id
     * @param page
     * @param pagesize
     * @return
     */
    public PageInfo<RecommendUser> findPageInfo(Long id, Integer page, Integer pagesize) {
        //通过远程调用获取推荐列表的数据
        PageInfo<RecommendUser> pageInfo = recommendUserApi.findPageInfo(id, page, pagesize);
        return pageInfo;
    }

    /**
     * 查看缘分值
     *
     * @param userId
     * @param toUserId
     * @return
     */
    public Double findScore(Long userId, Long toUserId) {
        Double score = recommendUserApi.findScore(userId, toUserId);
        if(ObjectUtil.isNotEmpty(score)){
            return score;
        }
        //如果获取的推荐值为空为其设置一个默认值
        return 98D;
    }
}
